﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WatiN.Core;
using QAHelper.Common;

namespace Sentrana_QA.WatiNScript.SpoonByteTestScript
{
    public static class RegistrationStepThree
    {
        private static RestaurantCommon restaurantCommon = new RestaurantCommon();
        static string registrationURL = Properties.Settings.Default.LOGIN_URL;

        public static string TestBRSS_B2_RR1_16(Browser window, DictionaryEntry[] tempTestData)
        {
            #region Local Parameters
            string result = null;
            int index = registrationURL.LastIndexOf('=');
            string syscoID = registrationURL.Substring(index + 1);           
            DictionaryEntry thisStepDictionary = new DictionaryEntry();
            int thisIndex = 0;
            if (tempTestData.Length > 1)
                thisIndex = 3;
            thisStepDictionary = tempTestData[thisIndex];    
            #endregion

            #region Step 3 Validation

            restaurantCommon.GoToStep3ByLogin(window);
            if (window.ContainsText(StepHeaderText.Step2HeaderMessage))
                result += Environment.NewLine+Environment.NewLine + QAHelper.Common.Enum.StatusText.Fail.ToString() + ":Need to complete Step 2 registration first in order to move to Step 3.";
            else
            {                   
         
                #region Working at Step 3
                if (window.ContainsText(StepHeaderText.Step3HeaderMessage))
                {
                    Button submit = window.Button(Find.ByValue(ButtonsName.SaveButton));

                    string dataSetHeader = Properties.Settings.Default.DATA_SET_DELIM_START +
                                           thisStepDictionary.Key.ToString() +
                                           Properties.Settings.Default.DATA_SET_DELIM_END;
                    if (tempTestData.Length > 1)
                    {
                        window.Button(Find.ByValue(ButtonsName.SaveButton)).Click();

                        result += dataSetHeader + Environment.NewLine +
                              Step3EmptyFieldValidation(window, CommonHelper.GetDataFromDictionary(tempTestData, 0),
                                                        submit);
                    

                        dataSetHeader = Properties.Settings.Default.DATA_SET_DELIM_START +
                                        tempTestData[1].Key.ToString() +
                                        Properties.Settings.Default.DATA_SET_DELIM_END;
                        result += dataSetHeader + Environment.NewLine +
                                  Step3InvalidCapacityFieldValidation(window,
                                                                      CommonHelper.GetDataFromDictionary(tempTestData, 1),
                                                                      submit); //WITH NEGATIVE VALUE

                        dataSetHeader = Properties.Settings.Default.DATA_SET_DELIM_START +
                                        tempTestData[2].Key.ToString() +
                                        Properties.Settings.Default.DATA_SET_DELIM_END;
                        result += dataSetHeader + Environment.NewLine +
                                  Step3InvalidCapacityFieldValidation(window,
                                                                      CommonHelper.GetDataFromDictionary(tempTestData, 2),
                                                                      submit); //WITH ALPHABET VALUE
                    }

                    dataSetHeader = Properties.Settings.Default.DATA_SET_DELIM_START + tempTestData[thisIndex].Key.ToString() + Properties.Settings.Default.DATA_SET_DELIM_END;
                    result += dataSetHeader + Environment.NewLine+Step3ValidData(window,tempTestData,submit);
                }
                else
                    result += Environment.NewLine+QAHelper.Common.Enum.StatusText.Fail.ToString() + ":Step 3 registration failed." + Environment.NewLine;
                #endregion
            }
            #endregion

            return result;
        }

        public static string TestBRSS_B2_RR1_17(Browser window, DictionaryEntry[] tempTestData)
        {
            #region Local Parameters
            string result = null;
            Div backgroundDiv;
            Button button;
            //string[] headings = { "Please select other cuisine types from the choices below that will be used to identify your restaurant’s secondary cuisine.",
            //                        "Please select the restaurant features from the choices below that accurately reflect your restaurant’s features.",
            //                        "Please select the \"Browse\" button to locate your menu on your computer. Click \"Upload\" to complete the menu upload process." };
            #endregion

            #region Step 3 Validation
                      

            restaurantCommon.GoToStep3ByLogin(window);

            if (window.ContainsText(StepHeaderText.Step2HeaderMessage))
                result += Environment.NewLine+Environment.NewLine + "Need to complete Step 2 registration first in order to move to Step 3.";
            else
            {
                window.Button(Find.ByValue(ButtonsName.SaveButton)).Click();
                if (window.ContainsText(StepHeaderText.Step3HeaderMessage))
                {
                    bool flag = true;
                    Div div;
                    backgroundDiv = window.Div(Find.ById("CoverBackgroundDiv"));

                    #region Other Cuisine Type
                    button = window.Button(Find.ByValue(ButtonsName.OtherCuisineButton));
                    button.Click();
                    div = window.Div(Find.ById("OtherCuisineListDiv"));

                    if (!window.ContainsText(PopupHeaderText.OtherCuisineTypeHeaderMessage) || !window.Div(Find.ById("OtherCuisineListDiv")).Enabled) result += Environment.NewLine+Environment.NewLine + "Other cuisine type pop-up does not appear.";
                    else if (div.Style.Display.ToString().ToLower() == "none") result += Environment.NewLine+Environment.NewLine + "Background is not disabled.";
                    else result += Environment.NewLine+Environment.NewLine + "Other cuisine type pop-up appears properly and the background is disabled.";

                    //Closing the pop-u                    
                    div.Button(Find.ByName("CancelButton")).Click();
                    if (!window.ContainsText(PopupHeaderText.OtherCuisineTypeHeaderMessage) || !window.Div(Find.ById("OtherCuisineListDiv")).Enabled)
                    {
                        result += Environment.NewLine+Environment.NewLine + QAHelper.Common.Enum.StatusText.Fail.ToString() + ":Other cuisine type pop-up does not closes.";
                        flag = false;
                    }
                    else if (div.Style.Display.ToString().ToLower() != "none")
                    {
                        result += Environment.NewLine+Environment.NewLine + QAHelper.Common.Enum.StatusText.Fail.ToString() + ":Background div does not disappear.";
                        flag = false;
                    }
                    #endregion

                    #region Restaurant Features
                    if (flag)
                    {
                        button = window.Button(Find.ByValue(ButtonsName.RestaurantFeaturesButton));
                        button.Click();
                        div = window.Div(Find.ById("FeatureListDiv"));

                        if (!window.ContainsText(PopupHeaderText.RestaurantFeaturesHeaderMessage) || !window.Div(Find.ById("FeatureListDiv")).Enabled) result += Environment.NewLine+Environment.NewLine + "Restaurant features pop-up does not appear.";
                        else if (div.Style.Display.ToString().ToLower() == "none") result += Environment.NewLine+Environment.NewLine + "Background is not disabled.";
                        else result += Environment.NewLine+Environment.NewLine + "Restaurant features pop-up appears properly and the background is disabled.";

                        //Closing the pop-up                        
                        div.Button(Find.ByName("CancelButtonFeature")).Click();

                        if (!window.ContainsText(PopupHeaderText.RestaurantFeaturesHeaderMessage) || !window.Div(Find.ById("FeatureListDiv")).Enabled)
                        {
                            result += Environment.NewLine+Environment.NewLine + QAHelper.Common.Enum.StatusText.Fail.ToString() + ":Restaurant features pop-up does not close.";
                            flag = false;
                        }
                        else if (div.Style.Display.ToString().ToLower() != "none")
                        {
                            result += Environment.NewLine+Environment.NewLine + QAHelper.Common.Enum.StatusText.Fail.ToString() + ":Background is not disabled.";
                            flag = false;
                        }
                    }
                    #endregion

                    #region Restaurant Menu
                    if (flag)
                    {
                        button = window.Button(Find.ByValue(ButtonsName.RestaurantMenuUploadButton));
                        button.Click();
                        div = window.Div(Find.ById("MenuUploaderDiv"));

                        if (!window.ContainsText(PopupHeaderText.RestaurantMenuHeaderMessage) || !window.Div(Find.ById("MenuUploaderDiv")).Enabled) result += Environment.NewLine+Environment.NewLine + "Restaurant menu pop-up does not appear.";
                        else if (div.Style.Display.ToString().ToLower() == "none") result += Environment.NewLine+Environment.NewLine + "Background is not disabled.";
                        else result += Environment.NewLine+Environment.NewLine + "Restaurant menu pop-up appears properly and the background is disabled.";

                        //Closing the pop-up                        
                        div.Button(Find.ByName("CancelButton")).Click();

                        if (!window.ContainsText(PopupHeaderText.RestaurantMenuHeaderMessage) || !window.Div(Find.ById("MenuUploaderDiv")).Enabled)
                        {
                            result += Environment.NewLine+Environment.NewLine + QAHelper.Common.Enum.StatusText.Fail.ToString() + ":Restaurant menu pop-up does not appear.";
                            flag = false;
                        }
                        else if (div.Style.Display.ToString().ToLower() != "none")
                        {
                            result += Environment.NewLine+Environment.NewLine + QAHelper.Common.Enum.StatusText.Fail.ToString() + ":Background is not disabled.";
                            flag = false;
                        }
                    }
                    #endregion

                }
                else
                    result += Environment.NewLine+QAHelper.Common.Enum.StatusText.Fail.ToString() + ":Pop-ups does not appear properly." + Environment.NewLine;
            }
            #endregion

            return result;

        }

        public static string TestBRSS_B2_RR1_19(Browser window, DictionaryEntry[] tempTestData)
        {
            #region Local Parameters
            string result = null, temp = null;
            bool flag = false;
            Div backgroundDiv;
            Button button;
            #endregion

            #region Step 3 Validation

            restaurantCommon.GoToStep3ByLogin(window);            

            #region Working at Step 3
            if (window.ContainsText(StepHeaderText.Step3HeaderMessage))
            {
                backgroundDiv = window.Div(Find.ById("CoverBackgroundDiv"));

                #region Other Cuisine Type
                button = window.Button(Find.ByValue(ButtonsName.OtherCuisineButton));
                button.Click();

                if (!window.ContainsText(PopupHeaderText.OtherCuisineTypeHeaderMessage) || !window.Div(Find.ById("OtherCuisineList")).Enabled)
                    result += Environment.NewLine+Environment.NewLine + QAHelper.Common.Enum.StatusText.Fail.ToString() + ":Other cuisine type pop-up does not appear.";
                else
                {
                    result += Environment.NewLine+Environment.NewLine + "Other cuisine type pop-up appears properly.";
                    ArrayList checkBoxes = new ArrayList();

                    #region Action
                    //Checking 5 Cuisines
                    for (int i = 0; i < 5; i++)
                    {
                        temp = "cuisine_" + new Random().Next(1, 20) + "_chk";
                        checkBoxes.Add(temp);
                        window.CheckBox(Find.ById(temp)).Checked = true;// Change();
                    }
                    window.Label(Find.ByClass("link_label")).Click();                    

                    //Checking whether all the checked boxes are cleared or not
                    for (int i = 0; i < 5; i++)
                    {
                        if (window.CheckBox(Find.ById(checkBoxes[i].ToString())).Checked)
                        {
                            result += Environment.NewLine+Environment.NewLine + QAHelper.Common.Enum.StatusText.Fail.ToString() + ":Clear form link does not work properly.";
                            flag = true;
                            break;
                        }
                    }

                    if (!flag) result += Environment.NewLine+Environment.NewLine + "Clear form link works properly.";
                    #endregion
                }
                #endregion
            }
            else
                result += Environment.NewLine+QAHelper.Common.Enum.StatusText.Fail.ToString() + ":Step 3 page does not appear properly." + Environment.NewLine;
            #endregion

            #endregion

            return result;
        }

        public static string TestBRSS_B2_RR1_20(Browser window, DictionaryEntry[] tempTestData)
        {
            #region Local Parameters
            string result = null;
            Div backgroundDiv;
            Button button;
            //string headings = "Please select the restaurant features from the choices below that accurately reflect your restaurant’s features.";
            Label popupLabel;

            #endregion

            #region Step 3 Validation

            restaurantCommon.GoToStep3ByLogin(window);

            #region Working at Step 3
            if (restaurantCommon.GoToStep3ByLogin(window))
            {
                int checkboxesCount = 0;
                string checkboxesPrefix = "feature_";
                string checkboxesSuffix = "_chk";
                bool isAllChecked = true;
                CheckBox checkBox;

                checkboxesCount = window.Div(Find.ById("FeatureListDiv")).CheckBoxes.Count;

                backgroundDiv = window.Div(Find.ById("CoverBackgroundDiv"));

                #region Feature pop-up action
                button = window.Button(Find.ByValue(ButtonsName.RestaurantFeaturesButton));
                button.Click();

                if (!window.ContainsText(PopupHeaderText.RestaurantFeaturesHeaderMessage) || (window.Div(Find.ById("FeatureListDiv")).Style.Display.ToLower() == "none"))
                    result += Environment.NewLine+Environment.NewLine + QAHelper.Common.Enum.StatusText.Fail.ToString() + ":Feature pop-up does not appear.";
                else
                {
                    result += Environment.NewLine+Environment.NewLine + "Feature pop-up appears properly.";

                    #region Select All Features
                    popupLabel = window.Label(Find.ByText("Select All Features"));
                    popupLabel.Click();

                    for (int i = 1; i <= checkboxesCount; i++)
                    {
                        if (!window.CheckBox(Find.ById(checkboxesPrefix + i + checkboxesSuffix)).Checked)
                        {
                            result += Environment.NewLine+QAHelper.Common.Enum.StatusText.Fail.ToString() + ":All check boxes not checked on clicking \"Select All Features\" link.";
                            isAllChecked = false;
                            break;
                        }
                    }
                    #endregion

                    #region Deselect All Features
                    if (!isAllChecked) //If all are not checked then need to check all checkboxes
                    {
                        for (int i = 1; i <= checkboxesCount; i++)
                        {
                            checkBox = window.CheckBox(Find.ById(checkboxesPrefix + i + checkboxesSuffix));
                            if (!checkBox.Checked) checkBox.Checked = true;
                        }
                    }
                    popupLabel = window.Label(Find.ByText("Deselect All Features"));
                    popupLabel.Click();

                    for (int i = 1; i <= checkboxesCount; i++)
                    {
                        if (window.CheckBox(Find.ById(checkboxesPrefix + i + checkboxesSuffix)).Checked)
                        {
                            result += Environment.NewLine+"All check boxes not unchecked on clicking \"Deselect All Features\" link.";
                            break;
                        }
                    }
                    #endregion
                }
                #endregion
            }
            else
                result += Environment.NewLine+"Step 3 page does not appear properly." + Environment.NewLine;
            #endregion

            #endregion

            return result;

        }


        #region Private Methods

        #region Invalid Capacity Field Value

        public static string Step3InvalidCapacityFieldValidation(Browser window, ArrayList data, Button submit)
        {
            string result = null;
            int noOfFields = 0;

            WatiNInputHelper.SetPageInput(window, data, out noOfFields);
            submit.Click();

            #region Error listing
            if (!window.ContainsText(InvalidFieldValueMessages.Capacity))
                result += Environment.NewLine+"Invalid Capacity value error message not shown.";
            else
                result = "Error message for invalid capacity value showed properly.";
            #endregion

            return result;
        }
        
        #endregion

        #region Step 3: Valid Data
        private static string Step3ValidData(Browser window, DictionaryEntry[] dataSet, Button submit)
        {
            string result = null;
            int noOfFields = 0;
            DictionaryEntry thisStepDictionary = new DictionaryEntry();
            int thisIndex = 0;

            if (dataSet.Length > 1)
                thisIndex = 3;

            Button primaryCuisine = window.Button((Find.ByValue("Select Primary Cuisine")));

            primaryCuisine.Click();

            window.RadioButton(Find.ByValue("36")).Checked = true;

            window.Button(Find.ByName("DoneButton")).Click();

            WatiNInputHelper.SetPageInput(window, CommonHelper.GetDataFromDictionary(dataSet, thisIndex), out noOfFields);

            submit.Click();


            #region Success verification
            if (!window.ContainsText(StepHeaderText.Step4HeaderMessage))
                result += Environment.NewLine+"Step 3 registration failed.";
            else
                result = "Step 3 registration successful.";
            #endregion

            return result;
        }
        #endregion

        #region Step 3: Empty Field Validation

        private static string Step3EmptyFieldValidation(Browser window, ArrayList data, Button submit)
        {
            string validationResult = string.Empty;
            int noOfFields = 0;

            WatiNInputHelper.SetPageInput(window, data, out noOfFields);
            submit.Click();

            #region Error listing
            if (!window.ContainsText(MessageInventory.PrimaryCuisineType)) validationResult += Environment.NewLine + "Primary Cuisine";
            if (!window.ContainsText(MessageInventory.Attire)) validationResult += Environment.NewLine + "Attire";
            if (!window.ContainsText(MessageInventory.AcceptCreditCard)) validationResult += Environment.NewLine + "Accept Credit Card";
            if (!window.ContainsText(MessageInventory.PriceRange)) validationResult += Environment.NewLine + "Price Range";
            if (!window.ContainsText(MessageInventory.Deliver)) validationResult += Environment.NewLine + "Deliver";
            if (!window.ContainsText(MessageInventory.TakeOut)) validationResult += Environment.NewLine + "Take out";
            if (!window.ContainsText(MessageInventory.TableService)) validationResult += Environment.NewLine + "Table service";
            if (!window.ContainsText(MessageInventory.Reservations)) validationResult += Environment.NewLine + "Reservations";
            if (!window.ContainsText(MessageInventory.Alcohol)) validationResult += Environment.NewLine + "Alcohol";

            if (validationResult == null)
                validationResult = "Error message shown properly." + validationResult;
            else
                validationResult = "Error message not shown properly.";
            #endregion

            return validationResult;
        }
        #endregion

        #endregion
    }
}
